﻿using System;
using BLWCMS.Core.IBusiness;
using BLWCMS.Core.Models;
using Simple.Data;

namespace BLWCMS.Core.Business
{
    public class MaterialBusiness : IMaterialBusiness
    {
        #region 添加素材

        /// <summary>
        /// 添加素材
        /// </summary>
        /// <param name="materialModel"></param>
        /// <returns></returns>
        public string Add(Models.MaterialModel materialModel)
        {
            if (materialModel.Type <= 0 || string.IsNullOrEmpty(materialModel.Title) ||
                string.IsNullOrEmpty(materialModel.Content))
            {
                return ResCode.N500("提交信息不完整！");
            }

            materialModel.AddDate = DateTime.Now;
            materialModel.Mid = (materialModel.AddDate.ToUniversalTime().Ticks - 621355968000000000)/10000000;

            try
            {
                var db = Database.OpenConnection(Dbcontext.ConnectionString);
                db.BL_Materials.Insert(materialModel);
                return ResCode.N200("添加成功！", "/material");
            }
            catch (Exception e)
            {
                return ResCode.N500(e.Message);
            }
        }

        #endregion

        #region 编辑素材

        /// <summary>
        /// 编辑素材
        /// </summary>
        /// <param name="materialModel"></param>
        /// <returns></returns>
        public string Edit(Models.MaterialModel materialModel)
        {
            if (materialModel.Type <= 0 || string.IsNullOrEmpty(materialModel.Title) ||
                string.IsNullOrEmpty(materialModel.Content))
            {
                return ResCode.N500("提交信息不完整！");
            }

            try
            {
                var db = Database.OpenConnection(Dbcontext.ConnectionString);
                db.BL_Materials.Update(materialModel);
                return ResCode.N200("编辑成功！", "/material");
            }
            catch (Exception e)
            {
                return ResCode.N500(e.Message);
            }
        }

        #endregion

        #region 删除素材

        /// <summary>
        /// 删除素材
        /// </summary>
        /// <param name="mid"></param>
        /// <returns></returns>
        public string Del(int mid)
        {
            if (mid > 0 && Convert.ToString(mid).Length >= 10)
            {
                try
                {
                    var db = Database.OpenConnection(Dbcontext.ConnectionString);
                    db.BL_Materials.DeleteByMid(mid);
                    return ResCode.N200("删除成功！", "/material");
                }
                catch (Exception e)
                {
                    return ResCode.N500(e.Message);
                }
            }
            else
            {
                return ResCode.N500("提交参数不合法！");
            }
        }

        #endregion

        #region 获取列表

        /// <summary>
        /// 获取列表
        /// </summary>
        /// <returns></returns>
        public System.Collections.Generic.List<Models.MaterialModel> GetList()
        {
            try
            {
                var db = Database.OpenConnection(Dbcontext.ConnectionString);
                return db.BL_Materials.All().OrderByAddDateDescending().ToList<Models.MaterialModel>();
            }
            catch (Exception)
            {
                return null;
            }
        }

        #endregion

        #region 获取模型

        /// <summary>
        /// 获取模型
        /// </summary>
        /// <param name="mid"></param>
        /// <returns></returns>
        public Models.MaterialModel GetModel(int mid)
        {
            if (mid > 0 && Convert.ToString(mid).Length >= 10)
            {
                try
                {
                    var db = Database.OpenConnection(Dbcontext.ConnectionString);
                    return (MaterialModel) db.BL_Materials.FindByMid(mid);
                }
                catch (Exception e)
                {
                    return null;
                }

            }
            else
            {
                return null;
            }
        }

        #endregion
    }
}
